package org.cloudserviceengineering.app;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/UserMangaService")
public class UserManageService_Tool extends HttpServlet {
	public static Map<String, String> userLogin = new HashMap<String, String>();

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(action);
		System.out.println(username);
		System.out.println(password);
		if (action.equals("login")) {
			String pwd = userLogin.get(username);
			if (pwd != null) {
				if (pwd.equals(password)) {
					System.out.println("ok");
					request.getRequestDispatcher("/Portal.jsp").forward(request, response);
				} else {
					System.out.println("no");
					request.setAttribute("alerts", "<div class=\" alert alert-dangaer\"role=\"alert\">登录失败</div>");
					request.getRequestDispatcher("/login.jsp").forward(request, response);
				}
			}else{
				System.out.println("no");
				request.setAttribute("alerts", "<div class=\" alert alert-dangaer\"role=\"alert\">登录失败</div>");
				request.getRequestDispatcher("/login.jsp").forward(request,response);
			}
		}
		if(action.equals("regist")){
			userLogin.put(username, password);
			System.out.println(userLogin.get(username));
			request.setAttribute("alerts", "<div class=\" alert alert-success\"role=\"alert\">注册成功</div>");
			request.getRequestDispatcher("/login.jsp").forward(request,response);
		}
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
		
	}

	
}
